# Customise this file, documentation can be found here:
# https://github.com/KrauseFx/fastlane/tree/master/docs
# vi: ft=ruby

$:.unshift File.dirname(__FILE__)
require 'lib/utils.rb'

fastlane_version "1.3"

default_platform :ios

platform :ios do
  before_all do
     ENV['DELIVER_WHAT_TO_TEST'] = git_commit_log
     ensure_git_status_clean
  end

  desc "Runs linting (and eventually static analysis)"
  lane :analyze do
    return if test_disabled?
    make 'lint'
  end

  desc "Runs all the unit tests."
  lane :test do
    return if test_disabled?
    # TODO: lint & test JS code
    xctest(
      scheme: 'Wikipedia',
      destination: "platform=iOS Simulator,name=iPhone 6,OS=8.3",
      reports: [
        {
          report: "html",
          output: "build/reports/unit-tests.html"
        },
        {
          report: "junit",
          output: "build/reports/unit-tests.xml"
        }
      ],
      clean: nil
    )
  end

  desc "Increment app build version, commit, & tag locally."
  lane :bump do
    with_bump
  end

  desc "Submit a new **Wikipedia Alpha** Build to Apple TestFlight"
  lane :alpha do
    increment_build_number(
      build_number: ENV["BUILD_NUMBER"].to_i
    )
    sigh(
      adhoc: false,
      force: true,
      filename: 'build/alpha.mobileprovision'
    )
    ipa(
      configuration: "Alpha",
      scheme: "Wikipedia Alpha",
      embed: 'build/alpha.mobileprovision',
      # needed to override automatic profile selection
      xcargs: "PROVISIONING_PROFILE=#{Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::SIGH_UDID]}"
    )
    hockey(
      notes: git_commit_log,
      notify: '0', # Means do not notify
      status: '1', # Means do not make available for download
    )
    deliver beta: true
  end

  desc "Submit a new **Wikipedia Beta** build to Apple TestFlight"
  lane :beta do
    sigh(
      adhoc: false,
      force: true,
      filename: 'build/beta.mobileprovision'
    )
    ipa(
      configuration: "Beta",
      scheme: "Wikipedia Beta",
      embed: 'build/beta.mobileprovision',
      xcargs: "PROVISIONING_PROFILE=#{Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::SIGH_UDID]}"
    )
    hockey(
      notes: git_commit_log,
      notify: '0', # Means do not notify
      status: '1', # Means do not make available for download
    )
    deliver beta: true
  end

  desc "Upload AdHoc build to Hockey."
  lane :rc do
    sigh(
      adhoc: true,
      force: true,
      filename: 'build/adhoc.mobileprovision'
    )
    # force iTunes file sharing to be enabled (normally disabled for release builds)
    ENV['WMF_FORCE_ITUNES_FILE_SHARING'] = '1'
    # force debug menu to be shown
    ENV['WMF_FORCE_DEBUG_MENU'] = '1'
    ipa(
      configuration: "Wikipedia",
      scheme: "Wikipedia",
      embed: 'build/adhoc.mobileprovision',
      xcargs: "PROVISIONING_PROFILE=#{Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::SIGH_UDID]}"
    )
    hockey(
      notes: git_commit_log,
      notify: '0', # Do not notify
      status: '2', # Make available for download
      release_type: '0' # 'beta' release type
    )
  end

  desc "Upload AppStore build to iTC & HockeyApp"
  lane :store do
    sigh(
      adhoc: false,
      force: true,
      filename: 'build/release.mobileprovision'
    )
    ipa(
      configuration: "Wikipedia",
      scheme: "Wikipedia",
      embed: 'build/release.mobileprovision',
      xcargs: "PROVISIONING_PROFILE=#{Fastlane::Actions.lane_context[Fastlane::Actions::SharedValues::SIGH_UDID]}"
    )
    hockey(
      notes: git_commit_log,
      notify: '0', # Do not notify
      status: '1', # Do not make available for download
      release_type: '1' # 'store' release type
    )
    deliver force: true
  end

  after_all do |lane|

  end

  error do |lane, exception|

  end
end
